import nodeResolve from "@rollup/plugin-node-resolve";
import fs, { outputFileSync } from "fs-extra";
import commonjs from "@rollup/plugin-commonjs";
import analyze from "rollup-plugin-analyzer";

// ! 不要妄想使用 importsmap 导入 @codingame/monaco-editor 或者是其他库，这些库没有后缀名，属于 nodejs 代码文件

fs.emptyDir("./dist/wrapper/");
// rollup.config.js
export default {
    external: [],
    input: "./lib/wrapper.js",
    output: {
        dir: "./dist/wrapper",
        format: "es",
    },
    treeshake: false,
    plugins: [
        {
            resolveDynamicImport(thisFile) {
                console.log("Language Package", thisFile);
                return {
                    external: "true",
                    id: thisFile.replace(
                        "./",
                        "https://cdn.jsdelivr.net/npm/@codingame/monaco-editor-wrapper/dist/"
                    ),
                };
            },
            resolveId(thisFile, importer) {
                // 把这个文件直接屏蔽

                if (["monaco-editor"].some((i) => thisFile.startsWith(i))) {
                    const id = thisFile.replace(
                        "monaco-editor/esm/vs",
                        "../editor"
                    );
                    return {
                        external: true,
                        id: id.endsWith(".js") ? id : id + ".js",
                    };
                }

                if (thisFile.startsWith("https://")) {
                    return false;
                }

                if (!/^[[\w:]|\.|\/]/.test(thisFile)) console.log(thisFile);
            },
            load(thisFile) {
                if (thisFile.endsWith("onig.wasm")) {
                    return `
                    const a ='https://cdn.jsdelivr.net/npm/vscode-oniguruma/release/onig.wasm';
                    export default a;`;
                }
            },
        },

        nodeResolve({
            browser: true,
            extensions: [".ts", ".tsx", ".js"],
        }),
        commonjs(),

        // babel({
        //     babelHelpers: "bundled",
        //     extensions: [".ts", ".tsx", ".js", ".svg"],
        //     exclude: ["**/@codingame/**/*"],
        // }),
        analyze({
            summaryOnly: true,
            writeTo: (str) =>
                outputFileSync("dist/analyze/wrapper.analyze.txt", str),
        }),
    ],
};
